package com.gcc.demo.controller;


import com.myboot.entity.User;
import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }


    //注册功能
    @RequestMapping("/myweb/reg")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if (username == null || password == null || nickname == null || ageStr == null || !ageStr.matches("[0-9]+")) {
            response.sendRedirect("/myweb/reg_input_error.html");
            return;
        }

        int age = Integer.parseInt(ageStr);
        System.out.println(username + "," + password + "" + nickname + "" + age);

        File userFile = new File(userDir, username + ".obj");
        if (userFile.exists()) {
            response.sendRedirect("/myweb/have_user.html");
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            User user = new User(username, password, nickname, age);
            oos.writeObject(user);
            response.sendRedirect("/myweb/reg_success.html");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    //登录功能
    @RequestMapping("/myweb/login")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || password == null) {
            response.sendRedirect("/myweb/login_input_error.html");
            return;
        }

        File userFile = new File(userDir, username + ".obj");
        if (userFile.exists()) {
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                User user = (User) ois.readObject();

                if (user.getPassword().equals(password)) {
                    response.sendRedirect("/myweb/login_success.html");
                    return;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        response.sendRedirect("/myweb/login_fail.html");

    }


    //用户列表
    @RequestMapping("/myweb/showAllUser")
    public void showAllUser(HttpServletRequest request, HttpServletResponse response) {
        List<User> userList = new ArrayList<>();
        File[] subs = userDir.listFiles(f -> f.getName().contains(".obj"));
        for (File userFile : subs) {
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                userList.add((User) ois.readObject());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>用户列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>用户名</td>");
        pw.println("<td>密码</td>");
        pw.println("<td>昵称</td>");
        pw.println("<td>年龄</td>");
        pw.println("<td>操作</td>");
        pw.println("</tr>");
        for (User user : userList) {
            pw.println("<tr>");
            pw.println("<td>" + user.getUsername() + "</td>");
            pw.println("<td>" + user.getPassword() + "</td>");
            pw.println("<td>" + user.getNickname() + "</td>");
            pw.println("<td>" + user.getAge() + "</td>");
            pw.println("<td><a href='/myweb/deleteUser?username=" + user.getUsername() + "'>删除</a></td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        System.out.println("页面生成完毕!");
        response.setContentType("text/html");
    }

    @RequestMapping("/myweb/deleteUser")
    public void deleteUser(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        File userFile = new File(userDir,username+".obj");
        userFile.delete();
        response.sendRedirect("/myweb/showAllUser");

    }

}
